<?php


namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;


/**
 * 示例接口
 */
class Member extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * @return void
     * 用户详情
     */
    public function getuserinfo()
    {
        $user=$this->auth->getUser();

        $user=Db::name('user')->where(['id'=>$user['id']])->find();


        if ($user['viper_time']<time()){

            $user['level']=1;
            Db::name('user')->where(['id'=>$user['id']])->update(['level'=>1,'viper_time'=>0]);
        }

        if ($user['level']!=2){
            $user['viper']='开通会员';
        }else{
            $user['viper']='会员到期:'. date('Y-m-d',$user['viper_time']);
        }


        $user['money']=round($user['money'],2);

        $this->success('用户详情',$user );

    }


    /**
     * 发布详情
     */
    /**
     * @return Auth
     */
    public function getinfobyid(){

        $user=$this->auth->getUser();
        $user=Db::name('user')->where(['id'=>$user['id']])->find();

        $id=$this->request->param('id');
        $info=Db::name('land')->where(['id'=>$id])->find();



        $show=0;
        if ($user['viper_time']>time()){
            $show=1;
        }


        $see['user_id']=['eq',$user['id']];
        $see['land_id']=['eq',$id];

        $like=Db::name('land_see')->where($see)->find();
        if (!$like){
            Db::name('land_see')->data(['land_id'=>$id,'user_id'=>$user['id']])->insert();
        }




        Db::name('land')->where(['id'=>$id])->setInc('seecount');


        $image=explode(',',$info['images']);

        foreach ($image as $key=>$v){
            $image[$key]=cdnurl($v,true);
        }

        $info['image']=cdnurl($info['image'],true);

        $info['images']=$image;


        $info['show']=$show;

        $pay['status']=['eq',1];
        $pay['user_id']=['eq',$user['id']];
        $pay['land_id']=['eq',$id];

        $pay=Db::name('land_pay')->where($pay)->find();


        if ($pay){
            $info['show']=1;
        }


        $info['sc']=0;
        $sc['user_id']=['eq',$user['id']];
        $sc['land_id']=['eq',$id];
        $like=Db::name('land_like')->where($sc)->find();

        if ($like){
            $info['sc']=1;
        }

        $info['times']=date('Y-m-d H:i',$info['times']);


        $info['mobile_v']=substr($info['landlordmobile'],0,4).'****'.substr($info['landlordmobile'],7,11);

        $this->success('用户详情',$info);



    }

    //收藏
    public function scland(){

        $user=$this->auth->getUser();

        $id=$this->request->param('id');

        $sc['user_id']=['eq',$user['id']];
        $sc['land_id']=['eq',$id];

        $like=Db::name('land_like')->where($sc)->find();



        if ($like){

            Db::name('land_like')->where(['id'=>$like['id']])->delete();
            $this->success('收藏',0);
        }else{


            Db::name('land_like')->data(['land_id'=>$id,'user_id'=>$user['id']])->insert();

            $this->success('收藏',1);
        }


    }

    /**
     * 设置用户信息
     */
    public function setuserinfo(){



        $user=$this->auth->getUser();

        $avatar=$this->request->param('avatar');
        $birthday=$this->request->param('birthday');
        $nickname=$this->request->param('nickname');


        $set['avatar']=$avatar;
        $set['birthday']=$birthday;
        $set['nickname']=$nickname;


        Db::name('user')->where(['id'=>$user['id']])->update($set);


        $this->success('操作成功!');
    }

    /**
     * 我的订单
     */

    public function getmyorder(){

        $user=$this->auth->getUser();
        $page=$this->request->param('page');

        $limits=($page-1)*10 .  ','. 10;

        $map['a.user_id']=['eq',$user['id']];
//        $map['a.status']=['eq',1];


        $banner=Db::name('land_pay')->alias('a')
            ->join('fa_land b','a.land_id=b.id')
            ->where('a.user_id','eq',$user['id'])
//            ->where('a.status','eq','1')
            ->field('a.*,b.name,b.image,b.money')
            ->order('a.id desc')
            ->limit($limits)
            ->limit('6')->select();


//        $order=Db::getLastSql();

        foreach ($banner as&$key){

            $key['times']=date('Y-m-d H:i',$key['times']);
            $key['image']=cdnurl($key['image'],true);
        }

        $this->success('我的订单?',$banner);

    }

    /**
     *  我的收藏
     */

    public function getmylike(){

        $user=$this->auth->getUserinfo();

        $page=$this->request->param('page');

        $limits=($page-1)*99 .  ','. 99;

        $map['a.user_id']=['eq',$user['id']];



        $banner=Db::name('land_like')->alias('a')
            ->join('fa_land b','a.land_id=b.id')
            ->where($map)
            ->field('b.*')
            ->limit($limits)
            ->limit('6')->select();

        foreach ($banner as&$key){

            $key['times']=date('Y-m-d H:i',$key['times']);
            $key['image']=cdnurl($key['image'],true);
        }

        $this->success('我的订单',$banner);

    }

    public function getmysee(){

        $user=$this->auth->getUserinfo();

        $page=$this->request->param('page');

        $limits=($page-1)*99 .  ','. 99;

        $map['a.user_id']=['eq',$user['id']];



        $banner=Db::name('land_see')->alias('a')
            ->join('fa_land b','a.land_id=b.id')
            ->where($map)
            ->field('b.*')
            ->limit($limits)
            ->limit('6')->select();

        foreach ($banner as&$key){

            $key['times']=date('Y-m-d H:i',$key['times']);
            $key['image']=cdnurl($key['image'],true);
        }

        $this->success('我的订单',$banner);


    }

    /**
     * 添加信息
     */
    public function addlands(){

        $user=$this->auth->getUserinfo();

        $info=$this->request->param();

        $info['times']=time();
        $info['user_id']=$user['id'];

        foreach ($info as $key=>$v){

            if (empty($v)){
                $this->error('请填写完整信息!');
            }

        }


        Db::name('land')->data($info)->insert();

        $this->success('操作成功',$info);
    }


    /**
     * 删除信息
     */

    public function deletegoods(){



        $id=$this->request->param('id');


        Db::name('land')->where(['id'=>$id])->delete();

        $this->success('删除信息');
    }


}


























